package vn.com.aptech.msh.controller;

import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import vn.com.aptech.msh.business.IEstateBean;
import vn.com.aptech.msh.business.IUserBean;
import vn.com.aptech.msh.entities.Advertising;
import vn.com.aptech.msh.utils.ApplicationAttributes;

/**
 *
 * @author trietto
 */
@Named("homepageController")
@RequestScoped
public class HomepageController implements Serializable {

    @Inject
    IUserBean userBusiness;
    @Inject
    IEstateBean estateBusiness;
    @Inject
    ApplicationAttributes applicationAttributes;

    private int views;
    private int advertisementCount;
    private List<Advertising> latestAdvertisements;
    private List<Advertising> featuredAdvertisements;

    @PostConstruct
    public void init() {
        views = applicationAttributes.getViews();
        advertisementCount = estateBusiness.getAdvertisementsCount();
        latestAdvertisements = estateBusiness.getLatestAdvertisements(4);
        featuredAdvertisements = estateBusiness.getFeaturedAdvertisements(7);
    }

    public int getViews() {
        return views;
    }

    public void setViews(int views) {
        this.views = views;
    }

    public int getUsersCount() {
        return userBusiness.getUsersCount();
    }

    public int getAdvertisementCount() {
        return advertisementCount;
    }

    public List<Advertising> getLatestAdvertisements() {
        return latestAdvertisements;
    }

    public List<Advertising> getFeaturedAdvertisements() {
        return featuredAdvertisements;
    }

    public String summarized(String source) {
        int maxLength = 200;
        if (maxLength > source.length()) {
            return source;
        } else {
            int i = maxLength - 1;
            while (i > 0) {
                if (source.charAt(i) != ' ') {
                    i--;
                }
            }
            String des = source.substring(0, i) + "...";
            return des;
        }
    }
}
